import $service from '../../service'
const app = getApp()
Component({
  properties: {
    isShow: {
      type: Boolean,
      value: true
    },
  },
  data: {
    idCard: '',
    patientPhone: ''
  },
  lifetimes: {
  },
  methods: {
    closeBox() {
      this.setData({
        isShow: false
      })
    },
    sure() {
      if (!this.data.idCard) {
        $service.showToast('请输入身份证号')
        return
      }
      if (!$service.validateElement('idCard', this.data.idCard)) {
        $service.showToast('请输入正确的身份证号');
        return;
      }
      if (!this.data.patientPhone) {
        $service.showToast('请输入手机号')
        return
      }
      if (!$service.validateElement('mobile', this.data.patientPhone)) {
        $service.showToast('请输入正确的手机号！');
        return;
      }
      wx.login({
        success: (e) => {
          if (e.code) {
            app.http.LoginByIdCard({
              code: e.code,
              idCard: this.data.idCard,
              patientPhone: this.data.patientPhone
            }).then(res => {
              let {list} = res.data
              $service.setStore('userInfo', JSON.stringify(list[0]));
              $service.setStore('miniToken', list[0].miniToken);
              this.getUserInfo() 
              this.judgeIsOrder()
            }).catch(err => {
              $service.showToast('登陆失败，请到【我的】使用账号密码登录')
            })
          } else {
            $service.showToast('登陆失败，请到【我的】使用账号密码登录')
          }
        }
      })
     
    },
    // 是否有药品订单
    judgeIsOrder() {
      app.http.judgeIsOrder().then(res => {
        let url = res.data.orderNum == 0 ? '/pages/user_side/personal/personal_prescription/personal_prescription' : '/pages/prescription_flow/order_list/order_list'
        wx.redirectTo({
          url: url,
        })
      }).catch(err => {
        $service.showToast(err.msg)
      })
    },
    // 缓存用户个人信息
    getUserInfo() {
        app.http.getUserInfo().then(res => {
            $service.setStore('userData', JSON.stringify(res.data));
        })
    },
    // 填写身份证
    inputCode(e) {
      let {type} = e.currentTarget.dataset
      let {value} = e.detail
      type == 'id' ? this.data.idCard = value : this.data.patientPhone = value
      this.setData({
        idCard: this.data.idCard,
        patientPhone: this.data.patientPhone
      })
    }
  } 
})
